package com.service.core.rest.response;

import com.github.pagehelper.Page;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.List;


/**
 * <dl>
 * <dt>platform</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2018</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019-04-14</dd>
 * </dl>
 */
@Setter
@Getter
@ToString
public class CommonPageRep<T> {

    private Integer pageNo;

    private Integer pageSize;

    private Integer totalCount;

    private List<T> data;


    public CommonPageRep() {

    }

    public CommonPageRep(Page<T> page) {
        this.pageNo = page.getPageNum();
        this.pageSize = page.getPageSize();
        this.totalCount = Math.toIntExact(page.getTotal());
        this.data = page.getResult();
    }

    public static <T> CommonPageRep<T> buildResultRep(Page<T> page) {
        return new CommonPageRep<>(page);
    }

    public static <T> CommonPageRep<T> buildResultRep(Page page, List<T> data){
        CommonPageRep<T> result = new CommonPageRep<>();
        result.pageNo = page.getPageNum();
        result.pageSize = page.getPageSize();
        result.totalCount = Math.toIntExact(page.getTotal());
        result.setData(data);
        return result;
    }
}
